package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;

public class CourseViewController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        int courseID;
        boolean questions;

        if (request.getParameter("mycourseid") != null) {
            courseID = Integer.parseInt(request.getParameter("mycourseid"));
        } else {
            courseID = Integer.parseInt(request.getParameter("tryoutid"));
        }

        LinkedList<User> teachers = queryManager.getAllCourseTeachers(courseID);

        if (request.getParameter("tryoutid") != null) {
            questions = false;
        } else {
            questions = queryManager.isSelftestbycourse(courseID);
        }

        request.setAttribute("hasquestions", questions);
        request.setAttribute("teachersUitSessie", teachers);
        request.setAttribute("mycourseid", courseID);


        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/course_view.jsp");
        dispatcher.forward(request, response);
    }
}
